<template>
  <ul class="am-pagination tpl-pagination" style="float:right" v-if="pageCount>1">
    <li><a href="javascript:;" v-on:click="toMin()">«</a></li>
    <template v-for="(i,index) in len">
      <li v-if="i==flag" class="am-active"><a href="javascript:;" v-on:click="toPage(i)">{{i+startIndex}}</a></li>
      <li v-else><a href="javascript:;" v-on:click=toPage(i)>{{i+startIndex}}</a></li>
    </template>
    <li><a href="javascript:;" v-on:click="toMax">»</a></li>
  </ul>
</template>
<script>
export default {
  name: "pageHandler",
  data () {
    return {
      startIndex: 0, //页码列表第一个下标，从0开始
      pageCount: 1, //总页码数
      flag: 1, //当前选中的页码在页码列表中的位置
      len: 1, //页码显示长度
      handler: ""
    }
  },
  methods: {
    init: function (conf) {
      this.handler = conf.handler;
    },
    count: function (count) {
      this.pageCount = parseInt(count);
      if ((this.flag + this.startIndex) > count) {
        this.len = this.len >= this.pageCount ? this.pageCount : 5;
        this.startIndex = this.pageCount - this.len;
        this.flag = this.len;
        return
      }
      if ((this.startIndex + this.len) > this.pageCount) {
        if (count < this.len) {
          this.len = parseInt(count);
        }
        var start = this.startIndex;
        this.startIndex -= ((start + this.len) - this.pageCount);
        if (this.flag > this.len)
          this.flag = this.len
        else
          this.flag += ((start + this.len) - this.pageCount);

        this.startIndex = parseInt(this.startIndex)
        this.flag = parseInt(this.flag);
        return
      }
      if (count > (this.startIndex + this.len)) {
        if (this.flag > 3) {
          this.startIndex += this.flag - 4;
          this.flag = 4;
        }
        if (this.len < 5) {
          this.len = count > 5 ? 5 : count
        }
        this.startIndex = parseInt(this.startIndex)
        this.len = parseInt(this.len)
        return
      }
    },
    toMax: function () { //跳转到最后一页
      this.len = this.len >= this.pageCount ? this.pageCount : 5;
      this.startIndex = this.pageCount - this.len;
      this.flag = this.len;
      this.$parent[this.handler](this.startIndex + this.flag)
    },
    toMin: function () { //跳转到第一页
      this.len = this.len >= this.pageCount ? this.pageCount : 5;
      this.startIndex = 0;
      this.flag = 1;
      this.$parent[this.handler](this.startIndex + this.flag)
    },
    toPage: function (i) { //跳转到某一页
      this.flag = i;
      var len = this.len;
      var mid = Math.ceil(len / 2);
      if (this.flag > mid) {
        this.startIndex += this.flag - mid;
        if (this.startIndex > this.pageCount - len) {
          this.startIndex = this.pageCount - len;
        } else {
          this.flag = mid;
        }
      } else {
        this.startIndex -= mid - this.flag
        if (this.startIndex < 0) {
          this.startIndex = 0;
        } else {
          this.flag = mid;
        }
      }
      this.$parent[this.handler](this.startIndex + this.flag)
    },
  }
}
</script>
